<template>
  <div class="box">
    <img src="../../../public/bg/login@3x.png" alt="" class="bg" />
    <img src="../../../public/logo/orange@3x.png" alt="" class="icon" />
    <div class="txt">
      <p>优 质 甄 选 · 高 枕 无 忧</p>
    </div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleFormRef"
      label-width="100px"
      class="login"
    >
      <el-form-item label="账号" prop="username">
        <el-input type="username" v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm()"
          >登录</el-button
        >
        <el-button>重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
    
    <script>
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules:{
        username: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
          ],
        password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
          ],
      },
      formSize: ""
    };
  },
  methods: {
    //登录
    submitForm() {
            this.$refs.ruleFormRef.validate((valid) => {
                if (valid) {
                    this.$api.reqLogin(this.ruleForm)
                        .then((res) => {
                          console.log(res);
                            // 1.存储用户登录信息
                            localStorage.setItem('userInfo', JSON.stringify(res.data.list))
                            // 2.提示用户登录成功 
                            this.$message({
                                message: '登录成功！',
                                type: 'success'
                            });
                            // 3.跳转到首页
                            this.$router.push("/index")
                        })
                        .catch(err => console.log(err))
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        }
  },
};
</script>
    
<style lang="less" scoped>
.box {
  width: 100%;
  height: 100%;
  background-color: #ff684c;
  box-sizing: border-box;
  position: relative;
  .bg {
    width: 50%;
    height: 80vh;
    margin-left: 500px;
    margin-top: 60px;
  }
  .icon {
    width: 200px;
    position: absolute;
    top: 150px;
    left: 830px;
  }
  .txt {
    position: absolute;
    top: 250px;
    left: 850px;
    p {
      color: #ccc;
    }
  }
  .login {
    position: absolute;
    left: 750px;
    top: 300px;
    width: 330px;
    .el-input {
      outline: 0;
      border: 0;
      margin-bottom: 10px;
      background-color: #ffefeb;
      padding-bottom: 10px;
      width: 100%;
    }
    .btn {
      width: 100%;
      border-radius: 20px;
      color: #fff;
      background-color: #ff684c;
      
    }
  }
}
</style>
    